package pl.edu.mimuw.irs.core.model.petrinet;

import java.util.HashSet;
import java.util.Set;

import pl.edu.mimuw.irs.core.exception.InternalException;
import pl.edu.mimuw.irs.core.exception.InternalException.Cause;
import pl.edu.mimuw.irs.core.util.Position;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-07
 */
public abstract class Node extends Element implements INode {

	private static final long serialVersionUID = -6271392860745827709L;
	
	// *** Attributes
	
	private String name;
	// Position of this node
	private Position position;
	// Arcs connected to this Place
	private Set<Arc> incomingArcs;
	private Set<Arc> outgoingArcs;
	
	private boolean detached; 
	// Getters and setters
	public String getName() {
		if (name == null) name = getId();
		return name;
	}
	public void setName(String name) {
		String oldName = this.name;
		this.name = name;
		getListeners().firePropertyChange(NAME, oldName, name);
	}
	@Override
	public Position getPosition() {
		if (position == null) position = new Position();
		return this.position;
	}
	public void setPosition(Position position) {
		Position oldPosition = this.position;
		this.position = position;
		getListeners().firePropertyChange(POSITION, oldPosition, position);
	}
	public Set<Arc> getIncomingArcs() {
		if (incomingArcs == null) incomingArcs = new HashSet<Arc>();
		return incomingArcs;
	}
	public void setIncomingArcs(Set<Arc> incomingArcs) {
		this.incomingArcs = incomingArcs;
	}
	public Set<Arc> getOutgoingArcs() {
		if (outgoingArcs == null) outgoingArcs = new HashSet<Arc>();
		return outgoingArcs;
	}
	public void setOutgoingArcs(Set<Arc> outgoingArcs) {
		this.outgoingArcs = outgoingArcs;
	}
	@Override
	public boolean isDetached() {
		return this.detached;
	}
	@Override
	public void setDetached(boolean detached) {
		this.detached = detached;
	}

	// *** Methods
	
	@Override
	public void detach() {
		if (isDetached()) 
			throw new InternalException(Cause.PETRI_NET_REPEATED_NODE_DETACH, this);
		setDetached(true);
	}

	@Override
	public String toString() {
		return getName() + "(" + getId() + ")";
	}
}
